//预算申请
function toAprBudget($scope, CertusFormAjax) {
    CertusFormAjax.ajax("findSeProList", {}, function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i = 0; i < data.length; i++) {
            var arr = {};
            arr.name = data[i].proName;
            arr.id = data[i].id;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete1').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    var budgetArr = [];

    this.getBudgetList = function () {
        var other2Columns = [{
            id: 'proName',
            title: '申请项目',
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'bugetType',
            title: '预算类型',
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'bugetMoney',
            title: '预算金额',
            type: 'string',
            width: 150,
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '￥' + value + '元';
                return content;
              }
        }, {
            id: 'useTimeSycle',
            title: '使用周期',
            type: 'string',
            width: 200,
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '';
                content += record.startTime + "至" + record.endTime;
                return content;
            }
        }, {
            id: 'comments',
            title: '费用说明',
            type: 'string',
            columnClass: 'text-center'
        }, {
            id: 'operation',
            title: '操作',
            type: 'string',
            width: 50,
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<button class="btn btn-xs btn-danger" onclick="delBuget(' + record.id + ');"><i class="fa fa-trash-o"></i>删除</button>';
                return content;
            }
        }];

        $("#other2Container").empty();
        var other2Option = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '其他费用',
            datas: budgetArr,
            columns: other2Columns,
            gridContainer: 'other2Container',
            tools: ''
        };
        var other1Grid = $.fn.dlshouwen.grid.init(other2Option);
        other1Grid.load();
    };

    var om = 1;
    this.addOtherMoney = function () {
        var bugetMoney = $("#bugetMoney").val();
        var comments = $("#comments").val();
        if (bugetMoney == "") {
            layer.msg("预算金额不能为空", { time: 1000 });
            return;
        }
        if (comments.trim() == "") {
            layer.msg("事由不能为空", { time: 1000 });
            return;
        }
        if (comments.length > 120) {
            layer.msg("事由不能超过120字", { time: 1000 });
            return;
        }
        var proId = $("#proId").val();
        if (proId == "") {
            layer.msg("申请项目不能为空", { time: 1000 });
            return;
        }

        var data = new Date();
        var datetime = $("#startTime").val();
        var startTime = new Date(datetime.replace("/", "-").replace("/", "-"));
        if (datetime == "") {
            layer.msg("开始日期不能为空", { time: 1000 });
            return;
        }
        var endTime = $("#endTime").val();
        if (endTime == "") {
            layer.msg("结束日期不能为空", { time: 1000 });
            return;
        }
        var endTimes = new Date(endTime.replace("/", "-").replace("/", "-"));
        if (startTime > endTimes) {
            layer.msg("开始日期不能大于结束日期", { time: 1000 });
            return;
        }

        var arr = {};
        arr.id = om;
        arr.bugetMoney = bugetMoney;
        arr.comments = comments;
        arr.bugetType = $("#bugetType").val();
        arr.startTime = $("#startTime").val();
        arr.endTime = $("#endTime").val();
        arr.proId = $("#proId").val();
        arr.proName = $("#autocomplete1").val();

        budgetArr.push(arr);
        om++;
        //刷新列表
        getBudgetList();

        clearBudget();

        //项目为第一次选中的项目
        if (typeof ($scope.proId) == "undefined" || $scope.proId == 0) {
            $scope.proId = arr.proId;
            $scope.proName = arr.proName;
            $("#autocomplete1").attr("disabled", "disabled");
        }
        $(".hideother").hide();
    };

    //删除
    this.delBuget = function (id) {

        layer.confirm('确认删除此数据？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            var dArr = {};
            for (var i = 0; i < budgetArr.length; i++) {
                if (budgetArr[i].id == id) {
                    dArr = budgetArr[i];
                }
            }
            budgetArr.splice($.inArray(dArr, budgetArr), 1);
            //刷新列表
            getBudgetList();

            if (budgetArr.length == 0) {
                $scope.proId = 0;
                $scope.proName = "";
                $("#proId").val(0);
                $("#autocomplete1").val("");
                $("#autocomplete1").attr("disabled", false);
            }
            layer.msg('删除成功');
        });


    };

    this.clearBudget = function () {
        $("#bugetType").val("客户交际费用");
        $("#bugetMoney").val("");
        $("#comments").val("");
        $("#startTime").val("");
        $("#endTime").val("");
    }
    $(".submit").on('click', function () {
        var totalBudget = 0;
        if (budgetArr.length == 0) {
            layer.msg("至少添加一条预算");
            return;
        } else {
            for (var i = 0; i < budgetArr.length; i++) {
                totalBudget += Number(budgetArr[i].bugetMoney);
            }
        }
        // 开启
        var index = layer.load(1);
        CertusFormAjax.ajax('addBudget', {
            "param": JSON.stringify(budgetArr),
            "proId": $scope.proId, "totalBudget": totalBudget
        }, function () {
            //关闭
            layer.close(index);
            layer.msg("申请成功");
            window.history.back();
        });
    })
}
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};